package com.hunliji.monitor.web.filter.auth;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Optional;

public class SpringContext {
    public static Optional<HttpServletRequest> getRequest() {
        ServletRequestAttributes servletRequestAttributes = Optional.ofNullable(((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())).orElse(null);
        if (servletRequestAttributes == null) {
            return Optional.empty();
        }

        return Optional.ofNullable(servletRequestAttributes.getRequest());
    }
    public static String getAuth() {
        HttpServletRequest httpServletRequest = getRequest().orElse(null);
        if (httpServletRequest == null) {
            return null;
        }

        return httpServletRequest.getHeader("token");
    }
}
